package com.auxgroup.smarthome.dto;

import com.auxgroup.smarthome.entity.RoleEntity;
import com.auxgroup.smarthome.entity.UserEntity;
import com.auxgroup.smarthome.utils.EncryptionUtils;
import org.hibernate.validator.constraints.NotEmpty;

import java.util.UUID;

/**
 * Created by kevinchen on 2017/2/22.
 */
public class UserDto {
    @NotEmpty(message = "用户名不能为空")
    private String userName;

    @NotEmpty(message = "密码不能为空")
    private String password;

    @NotEmpty(message = "真实姓名不能为空")
    private String realName;

    @NotEmpty(message = "手机号码不能为空")
    private String phone;

    @NotEmpty(message = "角色不能为空")
    private String roleId;

    public UserDto() {
    }

    public UserDto(String userName, String password, String realName, String phone, String roleId) {
        this.userName = userName;
        this.password = password;
        this.realName = realName;
        this.phone = phone;
        this.roleId = roleId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getRealName() {
        return realName;
    }

    public void setRealName(String realName) {
        this.realName = realName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRoleId() {
        return roleId;
    }

    public void setRoleId(String roleId) {
        this.roleId = roleId;
    }

    public UserEntity createUser(RoleEntity role){
        UserEntity userEntity = new UserEntity(this.userName,this.realName,this.phone,role);
        String salt = UUID.randomUUID().toString();
        userEntity.setSalt(salt);
        String password = EncryptionUtils.md5(this.password + salt );
        userEntity.setPassword(password);
        userEntity.setLoginFailCounter(0);
        return userEntity;
    }

}
